Grokking Google Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Google Easy
Add Binary (easy)
Solution: Add Binary
Valid Mountain Array (easy)
Solution: Valid Mountain Array
Set Mismatch (easy)
Solution: Set Mismatch
Is Subsequence (easy)
Solution: Is Subsequence
Length of Last Word (easy)
Solution: Length of Last Word
Search Insert Position (easy)
Solution: Search Insert Position
Repeated Substring Pattern (easy)
Solution: Repeated Substring Pattern
Monotonic Array (easy)
Solution: Monotonic Array
Count Nodes Equal to Average of Subtree (easy)
Solution: Count Nodes Equal to Average of Subtree
Find the Difference (easy)
Solution: Find the Difference
How Many Numbers Are Smaller Than the Current Number (easy)
Solution: How Many Numbers Are Smaller Than the Current Number
Binary Tree Paths (easy)
Solution: Binary Tree Paths
Find the Difference of Two Arrays (easy)
Solution: Find the Difference of Two Arrays
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Google Medium
Gray Code (medium)
Solution: Gray Code
2 Keys Keyboard (medium)
Solution: 2 Keys Keyboard
Arithmetic Slices (medium)
Solution: Arithmetic Slices
Unique Length-3 Palindromic Subsequences (medium)
Solution: Unique Length-3 Palindromic Subsequences
Minimum Difference Between Largest and Smallest Value in Three Moves (medium)
Solution: Minimum Difference Between Largest and Smallest Value in Three Moves
Letter Combinations of a Phone Number (medium)
Solution: Letter Combinations of a Phone Number
01 Matrix (medium)
Solution: 01 Matrix
Maximum Points You Can Obtain from Cards (medium)
Solution: Maximum Points You Can Obtain from Cards
Strobogrammatic Number II (medium)
Solution: Strobogrammatic Number II
Maximum Length of Semi-Decreasing Subarrays (medium)
Solution: Maximum Length of Semi-Decreasing Subarrays
Minimum Area Rectangle (medium)
Solution: Minimum Area Rectangle
Count Unreachable Pairs of Nodes in an Undirected Graph (medium)
Solution: Count Unreachable Pairs of Nodes in an Undirected Graph
Single Number II (medium)
Solution: Single Number II
4Sum II (medium)
Solution: 4Sum II
Furthest Building You Can Reach (medium)
Solution: Furthest Building You Can Reach
Minimizing Array After Replacing Pairs With Their Product (medium)
Solution: Minimizing Array After Replacing Pairs With Their Product
Fair Distribution of Cookies (medium)
Solution: Fair Distribution of Cookies
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Rotate Image (medium)
Solution: Rotate Image
Combination Sum (medium)
Solution: Combination Sum
Implement Trie (Prefix Tree) (medium)
Solution: Implement Trie (Prefix Tree)
Number of Islands (medium)
Solution: Number of Islands
Edit Distance (medium)
Solution: Edit Distance
Container With Most Water (Medium)
Solution: Container With Most Water
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Shortest Subarray with Sum at Least K (hard)
Solution: Shortest Subarray with Sum at Least K
Reverse Pairs (hard)
Solution: Reverse Pairs
Nth Magical Number (hard)
Solution: Nth Magical Number
Candy (hard)
Solution: Candy
Arithmetic Slices II - Subsequence (hard)
Solution: Arithmetic Slices II - Subsequence
Burst Balloons (hard)
Solution: Burst Balloons
Sum of Distances in Tree (hard)
Solution: Sum of Distances in Tree
Maximum Score of a Good Subarray (hard)
Solution: Maximum Score of a Good Subarray
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Solution: Meeting Rooms II

Problem Statement

Given an array of meeting intervals where intervals[i] = [starti, endi], return the minimum number of meeting rooms needed so that no meetings overlap.

Examples

Example 1:

  • Input: intervals = [[10, 15], [20, 25], [30, 35]]
  • Expected Output: 1
  • Justification: There are no overlapping intervals in the given list. So, only 1 meeting room is enough for all the meetings.

Example 2:

  • Input: intervals = [[10, 20], [15, 25], [24, 30], [5, 14], [22, 28], [1, 4], [27, 35]]
  • Expected Output: 3
  • Justification: Let's see how many meetings overlap at the same time:
    • [1, 4] starts first.
    • Then [5, 14] begins, no overlap yet.
    • [10, 20] overlaps with [5, 14]
    • [15, 25] overlaps with [10, 20]
    • [22, 28] overlaps with [15, 25]
    • [24, 30] overlaps with both [22, 28] and [15, 25]
    • [27, 35] overlaps with [24, 30]

Example 3:

  • Input: intervals = [[10, 20], [20, 30]]
  • Expected Output: 1
  • Justification: The end time of the first meeting is the same as the start time of the second meeting. So, one meeting can be scheduled right after the other in the same room.

Constraints:

  • 1 <= intervals.length <= 104
  • 0 <= starti < endi <= 106

Solution

To find the minimum number of meeting rooms required, we sort all the meetings by their start times. This allows us to always process meetings in chronological order. Then, we use a min-heap (priority queue) to track the end times of meetings that are currently using rooms. The heap helps us know which room becomes available the earliest.

For each meeting, we compare its start time with the earliest ending meeting (top of the heap). If the current meeting can reuse a room (its start time is greater than or equal to the earliest end time), we remove the ended meeting from the heap. Regardless, we then add the current meeting’s end time to the heap. At the end, the size of the heap tells us how many rooms were used at the same time — which is the minimum number of rooms required.

Step-by-Step Algorithm

  1. Check if there are no meetings:

    • If the input array intervals is empty, return 0 since no rooms are needed.
  2. Sort the meetings by their start times:

    • Use Arrays.sort() and sort all intervals based on the first element (start time).
  3. Create a min-heap (priority queue):

    • Initialize a PriorityQueue to keep track of ongoing meetings.
    • This heap will store the end times of meetings currently using a room.
    • The meeting that ends the earliest will always be at the top.
  4. Add the first meeting's end time to the heap:

    • This allocates the first room for the first meeting.
  5. Process the remaining meetings one by one:

    • For each meeting from the second onwards:
      • Compare its start time with the top of the heap (the earliest end time).
      • If the current meeting starts after or exactly when the earliest meeting ends:
        • Remove the top value from the heap (free up the room).
      • Add the current meeting’s end time to the heap (assign a room).
  6. Return the number of rooms used:

    • After processing all meetings, return the size of the heap.
    • This number represents the maximum number of rooms used at the same time, which is the minimum number of rooms required.

Algorithm Walkthrough

Step 1: Sort the intervals by start time

Sorted intervals:

[[1, 4], [5, 14], [10, 20], [15, 25], [22, 28], [24, 30], [27, 35]]

This ensures that we always process meetings in the order they begin.

Step 2: Initialize min-heap and add end time of the first meeting

  • We start by booking one room for the first meeting [1, 4].
  • We store its end time (4) in the heap to track when this room becomes free.
  • At this point, only one meeting is scheduled.

Heap status: [4]

Step 3: Process remaining meetings (from index 1 onward)

  • intervals[1] = [5, 14]

    • Start = 5
    • Earliest end in heap = 4
    • 5 >= 4 → ✅ Room is free
    • Remove 4 from heap (meeting [1, 4] has ended)
    • Add 14 to heap (allocate room for [5, 14])
    • Heap = [14]
  • intervals[2] = [10, 20]

    • Start = 10
    • Heap top = 14
    • 10 >= 14 → ❌ false
    • Room not free → Allocate new room
    • Add 20 to heap
    • Heap = [14, 20]
  • intervals[3] = [15, 25]

    • Start = 15
    • Heap top = 14
    • 15 >= 14 → ✅
    • Remove 14 from heap (free a room)
    • Add 25 to heap
    • Heap = [20, 25]
  • intervals[4] = [22, 28]

    • Start = 22
    • Heap top = 20
    • 22 >= 20 → ✅
    • Remove 20 from heap
    • Add 28 to heap
    • Heap = [25, 28]
  • intervals[5] = [24, 30]

    • Start = 24
    • Heap top = 25
    • 24 >= 25 → ❌
    • Need a new room
    • Add 30 to heap
    • Heap = [25, 28, 30]
  • intervals[6] = [27, 35]

    • Start = 27
    • Heap top = 25
    • 27 >= 25 → ✅
    • Remove 25 from heap
    • Add 35 to heap
    • Heap = [28, 30, 35]

Final Step: Return heap size

  • Final heap: [28, 30, 35]
  • Size = 3

This means 3 meeting rooms were used at peak overlap.

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

  • Time Complexity: The time complexity of our algorithm is , where (N) is the number of intervals. This is because we're sorting the intervals once and then using priority queues to process them.

  • Space Complexity: The space complexity is as we're storing all intervals in the worst case.

Meeting Rooms II (medium)
Shortest Subarray with Sum at Least K (hard)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis